﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using BLL;
using Utility;

public partial class SystemUI_UserUI_UserAMV : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {

            if (Request.QueryString["Type"] != null)
            {
                LoadService();
                if (Request.QueryString["Type"].ToString() == "View")
                {
                    txt_Name.ReadOnly = true;
                    rb_Enable.Enabled = false;
                    rb_DisEnable.Enabled = false;

                    btn_Add.Visible = false;
                    btn_Modity.Visible = false;
                    btn_ResetPwd.Visible = false;
                }
                else
                {
                    if (txt_Name.Text.Trim() == "admin")
                    {
                        //UtilityService.Alert(this.Page, "不能修改管理员信息!");
                        btn_Modity.Enabled = false;
                    }


                    txt_Name.ReadOnly = true;

                    btn_Add.Visible = false;
                    btn_Modity.Visible = true;
                    btn_ResetPwd.Visible = true;
                }
            }
            else
            {
                btn_Add.Visible = true;
                btn_Modity.Visible = false;
                btn_ResetPwd.Visible = false;
            }
        }
    }



    public void LoadService()
    {
        string uid = Request.QueryString["UID"].ToString();
        Model.User _p = new UserBLL().GetModel(uid);
        if (_p != null)
        {
            lab_Uid.Text = _p.UserID;
            txt_Name.Text = _p.UserName;
            if (_p.Status == 1)
            {
                rb_Enable.Checked = true;
            }
            else
            {
                rb_DisEnable.Checked = true;
            }
        }
    }
    protected void btn_Add_Click(object sender, EventArgs e)
    {
        if (txt_Name.Text.Trim().ToLower() == "admin")
        {
            UtilityService.Alert(this.Page,"admin是非法用户名，请换一个用户名称！");
            return;
        }
        

        Model.User u = new Model.User();
        u.UserID = lab_Uid.Text;
        u.UserName = txt_Name.Text.Trim();
        //u.Password = txt_Pwd.Text.Trim();
        u.Status = rb_Enable.Checked ? 1 : 0;
        u.OrganID = (int)Session["OrganID"];
        u.InputBy = Session["UserID"].ToString();

        DataSet oldU = new UserBLL().GetList(" UserName = '" + u.UserName + "' AND OrganID = " + u.OrganID);
        if (oldU != null && oldU.Tables[0].Rows.Count > 0)
        {
            UtilityService.Alert(this, "该用户名已存在!");
            return;
        }

        int re = new UserBLL().Add(u);
        if (re > 0)
        {
            UtilityService.AlertAndRedirect(this, "添加成功!", "UserMgr.aspx");
        }
        else
        {
            UtilityService.Alert(this, "添加失败!");
        }
    }
    protected void btn_Modity_Click(object sender, EventArgs e)
    {
        Model.User u = new Model.User();
        u.UserID = lab_Uid.Text;
        u.UserName = txt_Name.Text.Trim();
        u.OrganID = (int)Session["OrganID"];
        u.Status = rb_Enable.Checked ? 1 : 0;
        bool re = new UserBLL().Update(u);
        if (re)
        {
            UtilityService.AlertAndRedirect(this, "修改成功!", "UserMgr.aspx");
        }
        else
        {
            UtilityService.Alert(this, "修改失败!");
        }
    }
    protected void btn_ResetPwd_Click(object sender, EventArgs e)
    {
        bool re = new UserBLL().UpdatePwd(lab_Uid.Text.Trim(), "123456");
        if (re)
        {
            UtilityService.AlertAndRedirect(this, "初始化密码成功!", "UserMgr.aspx");
        }
        else
        {
            UtilityService.Alert(this, "初始化密码失败!");
        }
    }
}
